package user

import (
	"goblog/app/models"
	password2 "goblog/pkg/password"
	"goblog/pkg/route"
)

type User struct {
	models.BaseModel

	Name     string `gorm:"column:name;type:varchar(100);not null;unique" valid:"name"`
	Email    string `gorm:"column:email;type:varchar(100);default null;unique" valid:"email"`
	Password string `gorm:"column:password;type:varchar(255)" valid:"password"`
	// gorm:"-" 设置gorm 在读写时略过此字段
	PasswordConfirm string `gorm:"-" valid:"password_confirm"`
}

// ComparePassword 对比密码是否匹配
func (user *User) ComparePassword(password string) bool {
	return password2.CheckHash(password, user.Password)
}

// Link 方法用来生成用户链接
func (user User) Link() string {
	return route.Name2URL("users.show", "id", user.GetStringID())
}
